package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.ItemCompra;
import entity.ItemParcela;
import entity.TipoProduto;

public class ItemCompraDaoImplementation implements ItemCompraDao {

	public void save(ItemCompra itemCompra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(itemCompra);
		transaction.commit();
		session.close();
	}

	public void update(ItemCompra itemCompra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(itemCompra);
		transaction.commit();
	}

	public void delete(ItemCompra itemCompra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(itemCompra);
		transaction.commit();
	}

	public void consult(ItemCompra itemCompra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		itemCompra = (ItemCompra) session.load(ItemCompra.class, new String("ItemCompra"));
	}

	public ItemCompra getItemCompra(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (ItemCompra) session.load(ItemCompra.class, id);
	}
	
	public List<ItemCompra> listCompra() 
	{
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();
	String sql = "SELECT new ItemCompra( i.idItemCompra, i.quantidade, i.produto) FROM ItemCompra i";
	List<ItemCompra> lista = session.createQuery(sql).list();
	transaction.commit();
	session.close();
	return lista;
	}
	
	@Override
	public List<ItemCompra> list() {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "FROM ItemCompra WHERE compra="+null;
        List<ItemCompra> listaCompra = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return listaCompra;
	}
	

}
